/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd.            */
/*                                                                 */
/*  NAME      = t__sqlserver_bigint_multi.c                        */
/*  NOTE      =                                                    */
/*  DATE      = 2014/11/25 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include <frmem.h>

#include <sqlserver/config.h>
#include <sqlserver/sqlserver_base_type.h>

/* 1900年1月1号到1970年一月一号相隔的天数 */
#define TDS_1900_1970_DAYS                25567
/* 4位最大整数 */
#define MSSQL_4BYTE_MAX_VALUE             "4294967296"
/* 8位最大整数 */
#define MSSQL_8BYTE_MAX_VALUE             "18446744073709551616"
/* 12位最大整数 */
#define MSSQL_12BYTE_MAX_VALUE            "340282366920938463463374607431768211456"
/* 16位最大整数的值为115792089237316195423570985008687907853269984665640564039457584007913129639935，长度78 */
#define MSSQL_16BYTE_MAX_LEN              78

/*******************************************************************************
*  FUNC     :  大整数相乘
*  ARGS     :  p_multiplier(乘数)
*           :  p_multiplicand(被乘数)
*           :  p_result(乘积)
*  RTN      :  void
*  NOTE     :  适用与本.c文件内部调用，若需要共同出来，则需要扩张最大长度的值
*******************************************************************************/
extern void __sqlserver_bigint_multi(const char *p_multiplier, char *p_multiplicand, char *p_result);

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    char *p_multiplier = (char *) "0";
    char *p_multiplicand = (char *) "0";
    char *p_result = NULL;

    p_result = (char *) frmalloc(MSSQL_16BYTE_MAX_LEN + 1);

    __sqlserver_bigint_multi(p_multiplier, p_multiplicand, p_result);

    CU_ASSERT_STRING_EQUAL(p_result, "0");
    frfree(p_result);

    return;
}

static void __case2(void)
{
    char *p_multiplier = (char *) "90496544199165";
    char *p_multiplicand = (char *) "546";
    char *p_result = NULL;

    p_result = (char *) frmalloc(MSSQL_16BYTE_MAX_LEN + 1);

    __sqlserver_bigint_multi(p_multiplier, p_multiplicand, p_result);

    CU_ASSERT_STRING_EQUAL(p_result, "49411113132744090");
    frfree(p_result);

    return;
}

static void __case3(void)
{
    char *p_multiplier = (char *) "0";
    char *p_multiplicand = (char *) "546";
    char *p_result = NULL;

    p_result = (char *) frmalloc(MSSQL_16BYTE_MAX_LEN + 1);

    __sqlserver_bigint_multi(p_multiplier, p_multiplicand, p_result);

    CU_ASSERT_STRING_EQUAL(p_result, "0");
    frfree(p_result);

    return;
}

static void __case4(void)
{
    char *p_multiplier = (char *) "90496544199165";
    char *p_multiplicand = (char *) "0";
    char *p_result = NULL;

    p_result = (char *) frmalloc(MSSQL_16BYTE_MAX_LEN + 1);

    __sqlserver_bigint_multi(p_multiplier, p_multiplicand, p_result);

    CU_ASSERT_STRING_EQUAL(p_result, "0");
    frfree(p_result);

    return;
}

int add_suite__sqlserver_bigint_multi(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __sqlserver_bigint_multi()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    //增加测试用例
    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
